package com.xiebishe.mylibrary.net.retrofit;



import com.alibaba.fastjson.JSONException;

import java.io.InterruptedIOException;
import java.net.ConnectException;

import retrofit2.HttpException;


/**
 * @class ExceptionHandle
 * @describe 将原本的错误Throwable进一步判断类型，封装成自己的错误ResponeThrowable，内部包含errorCode(错误类型)和error
 */
public class ExceptionHandle {

    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static ResponeThrowable handleException(Throwable e) {
        ResponeThrowable ex;
        //网络错误
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;

            switch (httpException.code()) {
                case UNAUTHORIZED:
                    ex = new ResponeThrowable(e, RxERROR.UNAUTHORIZED_ERROR);
                    ex.message = "未授权";
//                    AccountManager.getInstance().logout(MyActivityManager.getInstance().getCurrentActivity(), false);
                    break;
                case FORBIDDEN:
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    ex = new ResponeThrowable(e, RxERROR.HTTP_ERROR);
                    ex.message = "网络请求错误";
                    break;
            }
            return ex;
            //网络正确，但是服务器返回的状态码不对，属于协议内的错误
        } else if (e instanceof CustomException) {
            CustomException customException = (CustomException) e;
            ex = new ResponeThrowable(customException, customException.code);
            ex.message = customException.message;
            return ex;
        } else if (e instanceof JSONException) {
            ex = new ResponeThrowable(e, RxERROR.PARSE_ERROR);
            ex.message = "数据解析错误";
            return ex;
        } else if (e instanceof ConnectException ||
                e instanceof InterruptedIOException) {
            ex = new ResponeThrowable(e, RxERROR.NETWORK_ERROR);
            ex.message = "can not connect";
            return ex;
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            ex = new ResponeThrowable(e, RxERROR.SSL_ERROR);
            ex.message = "证书验证失败";
            return ex;
        } else {
            ex = new ResponeThrowable(e, RxERROR.UNKNOWN);
            ex.message = "未知错误";
            return ex;
        }
    }


    public static class ResponeThrowable extends Exception {
        public int errorCode;
        public String message;

        public ResponeThrowable(Throwable throwable, int errorCode) {
            super(throwable);
            this.errorCode = errorCode;
        }

        public String getResponseMessage() {
            return message;
        }
    }

    public static class CustomException extends RuntimeException {
        public int code;
        public String message;

        public CustomException(int code, String message) {
            this.code = code;
            this.message = message;
        }
    }
}
